/*
 * Amendment History:
 * 
 * Date       By         Description 
 * ----------------------------------------------- 
 * Mar 24, 2010 Anselm Hou    Create the class
 */
package ctn.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;
public enum MimeContentType
{
   PDF("application/pdf"),
   XML("text/xml"),
   VXF("text/vxf"),
   XLS("application/x-msexcel"),
   CSV("text/csv"),
   JPG("image/jpeg"),
   GIF("image/gif"),
   BMP("image/bmp"),
   PNG("image/x-png"),
   HTML("text/html"),
   JSON("application/json"),
   JAVASCRIPT("application/javascript"),
   ZIP("application/zip"),
   TXT("text/plain"),
   CSS("text/css"),
   RESOURCE_BASE_MIMETYPE("");
   
   private String contentType;
   
   MimeContentType(String type){
      this.contentType=type;
   }
   
   public String getContentType(){
      return this.contentType;
   }
   
   
   
   
   @Override
   public String toString()
   {
      return getContentType();
   }
   
   public static MimeContentType getContentType(File file){
      
      String extension=file.getName().split("[.]")[1];
      if("PDF".equalsIgnoreCase(extension)){
         return PDF;
      }
      else if("XML".equalsIgnoreCase(extension)){
         return XML;
      }
      else if("VXF".equalsIgnoreCase(extension)){
         return VXF;
      }
      else if("XLS".equalsIgnoreCase(extension)){
         return XLS;
      }      
      else if("CSV".equalsIgnoreCase(extension)){
         return CSV;
      }   
      else if("JPG".equalsIgnoreCase(extension)){
         return JPG;
      }   
      else if("GIF".equalsIgnoreCase(extension)){
         return GIF;
      }         
      else if("BMP".equalsIgnoreCase(extension)){
         return BMP;
      }   
      else if("PNG".equalsIgnoreCase(extension)){
         return PNG;
      }       
      else if("HTML".equalsIgnoreCase(extension)){
         return HTML;
      }         
      else if("JSON".equalsIgnoreCase(extension)){
         return JSON;
      }        
      else if("JS".equalsIgnoreCase(extension)){
         return JAVASCRIPT;
      }   
      else if("ZIP".equalsIgnoreCase(extension)){
         return ZIP;
      }         
      else if("ZIP".equalsIgnoreCase(extension)){
         return ZIP;
      }          
      else if("CSS".equalsIgnoreCase(extension)){
    	  return CSS;
      }else if("SQL".equalsIgnoreCase(extension)){
    	  return TXT;
      }
      else{
         MimeContentType contentType=RESOURCE_BASE_MIMETYPE;      
         try
         {
            InputStream is=new FileInputStream(file);         
            try
            {
               ContentHandler contenthandler = new BodyContentHandler();
               Metadata metadata = new Metadata();
               metadata.set(Metadata.RESOURCE_NAME_KEY, file.getName());
               Parser parser = new AutoDetectParser();
               // OOXMLParser parser = new OOXMLParser();
               parser.parse(is, contenthandler, metadata);
               
               String mimeType=metadata.get(Metadata.CONTENT_TYPE);
               contentType.setContentType(mimeType);
               
               return contentType;
            }
            finally
            {
               is.close();
            }
         }
         catch (Exception ex)
         {
        	 ex.printStackTrace();
         }
      
    	  return null;
    	  }
      
   }
   
   
   /**
    * @param contentType set the value of contentType
    */
   private void setContentType(String contentType)
   {
      this.contentType = contentType;
   }
}
